#pragma once

#include "Validator.h"
#include <opencv2/opencv.hpp>

std::vector<std::string> split(const std::string &str, const std::string &delim);

class ValidatorWithFincon : public Validator
{
private:
    //ugly code,who is responsible for deleting map.values ?
    map<string, int *> fincon_map;
    const string fincon_save_file_path = "/home/yjy/dataset/palmprint_dectection_tongji/LHand/txt_data/Figcon/figCon_nn.txt";

    virtual fincon *generate_fincon() const
    {
        char buff[200000];

        ifstream txt(fincon_save_file_path);
        txt.read(buff, 200000);
        string roi(buff);
        vector<string> rows = split(roi, "\n");
        fincon *fc = new fincon[rows.size()];

        for (int i = 0; i < rows.size(); i++)
        {
            //assert that rows.size==db.size
            fc[i].deserialize(rows[i]);
        }

        return fc;
    }

protected:
    Database &db;

    virtual int *get_fincon_coordinates(const palmprint &p) const
    {
        //int *fincon = new int[6];fincon_map
        return this->fincon_map.at(p.filename);
    }

public:
    ValidatorWithFincon(Database &db, string fincon_file_path) : Validator(db), fincon_save_file_path(fincon_file_path)
    {
        //initialize Fincon map
        fincon *fc = generate_fincon();
        for (int i = 0; i < 3000; i++) //TODO: ugly code, db.size()
        {
            string filename = fc[i].filename;
            int *fincon_coordinates = new int[8];
            for (int j = 0; j < 8; j++)
            {
                fincon_coordinates[j] = fc[i].coordinates[j];
            }
            fincon_map.insert(std::pair<string, int *>(filename, fincon_coordinates));
        }
        delete[] fc;
    };

    ~ValidatorWithFincon();
};

Validator::~Validator()
{
    //todo:clear fincon_map
}
